﻿using Microsoft.Extensions.Options;
using System.Net.Mail;

namespace ApplicationBase.Core.Email
{
    public class SMTPEmailService : IEmailService
    {
        private readonly SMTPEmailOptions _options;
        public SMTPEmailService(IOptions<SMTPEmailOptions> options)
            => _options = options.Value;

        public void Send(MailMessage message)
        {
            using var smtpClient = new SmtpClient();
            
            if (!string.IsNullOrEmpty(_options.Host)) 
                smtpClient.Host = _options.Host;
            
            if (_options.Port > 0)
                smtpClient.Port = _options.Port;

            if (_options.IsSaveLocal)
            {
                if (string.IsNullOrEmpty(_options.SaveLocalPath))
                    throw new ArgumentNullException("[option:SaveLocalPath] can't be empty, where [option:IsSaveLocal] set true.");

                smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                smtpClient.PickupDirectoryLocation = _options.SaveLocalPath;
            }
            
            smtpClient.Send(message);
        }
    }
}
